package org.dstart.object.color.sub
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class ColorListObject extends Sprite
	{
		private var m_objArray:Array;			//	颜色对象数组
		private var m_topSprite:Sprite;			//	外框影片
		private var m_nowCaseColor:uint;		//	当前选择的颜色
		
		private var m_size:Number = 10;		//	边长值
		
		public function ColorListObject(numSize:Number, xx:Number = 0, yy:Number = 0)
		{
			m_size = numSize;
			
			this.x = xx;
			this.y = yy;
			this.m_nowCaseColor = 0x000000;
			
			ShowColorList();
			DrawRectangle();
			SetListener();
		}
		
		/**　读取颜色值 */
		public function get color():uint
		{
			var tObject:SubColorObject = GetObject();
			
			if(tObject == null) {
				return m_nowCaseColor;
			} else {
				return tObject.color;
			}
		}
		
		/**	读取当前设置的颜色值 */
		public function get now():uint
		{
			return m_nowCaseColor;
		}
		
		/** 设置当前的颜色 */
		public function set now(col:uint):void
		{
			m_nowCaseColor = col;
		}
		
		////////////////////////////////////////////////////////////////////////////
		
		/** 绘制 216 个选色块 */
		private function ShowColorList():void
		{
			var xDep:Number = 0;
			var yDep:Number = 0;
			var dep:Number = 0;
			
			m_objArray = new Array();
			m_objArray[xDep] = new Array();
			
			for(var i:int = 0x00; i <= 0xFF; i+= 0x33)
			{
				for(var j:int = 0x00; j <= 0xFF; j+= 0x33)
				{
					for(var k:int = 0x00; k <= 0xFF; k+= 0x33)
					{
						var numColor:uint = i * 0x100 * 0x100 + j * 0x100 + k;						
						
						dep ++;						
						
						if(Math.floor((dep-1) / 6) > xDep) {
							xDep++;
							yDep = 0;
							m_objArray[xDep] = new Array();
						}
						
						if(dep <= 108) {
							m_objArray[xDep][yDep] = new SubColorObject(numColor, xDep * m_size, yDep * m_size, m_size); 
							this.addChild(m_objArray[xDep][yDep]);						
						} else {							
							m_objArray[xDep - 18][yDep + 6] =  new SubColorObject(numColor, (xDep - 18) * m_size, (yDep + 6) * m_size, m_size); 
							this.addChild(m_objArray[xDep - 18][yDep + 6]);						
						}
						
						yDep ++;
					}
				}
			}
		}
		
		/** 绘制外部框线 */
		private function DrawRectangle():void
		{			
			m_topSprite = new Sprite();
			m_topSprite.graphics.lineStyle(1, 0xFFFFFF, 1, true);
			m_topSprite.graphics.drawRect(0, 0, m_size, m_size);
			m_topSprite.visible = false;
			addChild(m_topSprite);
		}
		
		/** 设置事件侦听 */
		private function SetListener():void
		{
			this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseIn);
			this.addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
			// this.addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		/** 取得当前鼠标所在位置的对象名称 */
		private function GetObject():SubColorObject
		{
			var xx:int = Math.floor(this.mouseX / m_size);
			var yy:int = Math.floor(this.mouseY / m_size);			
			
			if(xx >= 0 && xx < m_objArray.length) {
				if(yy >= 0 && yy < m_objArray[xx].length) {
					return m_objArray[xx][yy];
				} 
			} 
			
			return null;				
		}
		
		/** 鼠标移动操作（显示白色外框） */
		private function onMouseIn(e:MouseEvent):void
		{
			var tObject:SubColorObject = GetObject();
			
			if(tObject != null) {
				m_topSprite.x = tObject.x;
				m_topSprite.y = tObject.y;
				m_topSprite.visible = true;
			} else {
				m_topSprite.visible = false;
			}
		}
		
		/** 鼠标移出热响应区（隐藏白色外框）*/
		private function onMouseOut(e:MouseEvent):void
		{
			m_topSprite.visible = false;
		}
		
		/** 设置鼠标点击事件 */
		//private function onMouseClick(e:MouseEvent):void
		//{
		//	m_nowCaseColor = this.color;
		//}		
	}	
}